package com.sumansoul.photodemo;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateTimeHelper {

    private static final String DATETIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
    private static final String TIME_FORMAT_PATTERN = "HH:mm:ss";
    private static final String DATETIME_MILLSECONDS_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";

    public DateTimeHelper() {
    }

    public static String formatDateTime(Date value) {
        return format(value, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatDate(Date value) {
        return format(value, "yyyy-MM-dd");
    }

    public static String formatTime(Date value) {
        return format(value, "HH:mm:ss");
    }

    public static String formatMillSecondDateTime(Date value) {
        return format(value, "yyyy-MM-dd HH:mm:ss.SSS");
    }

    public static String format(Date value, String formatString) {
        if (value == null) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(formatString, Locale.getDefault());
            return sdf.format(value);
        }
    }

    public static int getSecondTimeStamp(Date value) {
        long time = value.getTime();
        String timestamp = String.valueOf(time / 1000L);
        return Integer.valueOf(timestamp);
    }

    public static long getCurrentTimeStamp() {
        return (new Date()).getTime();
    }

    public static int getCurrentSecondTimeStamp() {
        return getSecondTimeStamp(new Date());
    }

    public static Date getYesterdayDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(5, -1);
        Date yesterday = calendar.getTime();
        return yesterday;
    }

    public static Date getLastDayOfLastMonth(Integer month, Integer flag) {
        Calendar cal = Calendar.getInstance();
        cal.add(2, month);
        int startDay = cal.getActualMinimum(5);
        int endDay = cal.getActualMaximum(5);
        if (flag != null && flag >= 0 && flag <= 0) {
            cal.set(cal.get(1), cal.get(2), startDay);
        } else {
            cal.set(cal.get(1), cal.get(2), endDay);
        }

        return cal.getTime();
    }

    public static String getDateOrStampByMonth(Integer month, Integer flag) {
        String dateOrStamp = "";

        try {
            Calendar calendar = Calendar.getInstance();
            calendar.add(2, month);
            Date yesterday = calendar.getTime();
            if (flag != null) {
                if (flag == 2) {
                    dateOrStamp = (new SimpleDateFormat("yyyy-MM-dd")).format(yesterday);
                } else {
                    dateOrStamp = yesterday.getTime() + "";
                }
            } else {
                dateOrStamp = yesterday.getTime() + "";
            }

            return dateOrStamp;
        } catch (Exception var5) {
            return "err";
        }
    }

    public static Long dateToStamp(String dateStr) {
        Long ts = 0L;

        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (dateStr.trim().length() == 10) {
                dateStr = dateStr + " 00:00:00";
            }

            Date date = simpleDateFormat.parse(dateStr);
            ts = date.getTime();
            return ts;
        } catch (Exception var4) {
            return 0L;
        }
    }

    public static Date addDay(int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(5, day);
        Date afterAddDate = calendar.getTime();
        return afterAddDate;
    }

    public static Date addDay(Date startDate, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(5, day);
        Date afterAddDate = calendar.getTime();
        return afterAddDate;
    }

    public static String getYesterdayString() {
        Date yesterday = getYesterdayDate();
        String yesterdayStr = formatDate(yesterday);
        return yesterdayStr;
    }

    public static String getDateStringByFormat(String format) {
        Date date = new Date();
        return format(date, format);
    }

    public static String getCurrentYear() {
        Date current = new Date();
        return format(current, "yyyy");
    }

    public static String getCurrentMonth() {
        Date current = new Date();
        return format(current, "MM");
    }

    public static String getCurrentDay() {
        Date current = new Date();
        return format(current, "dd");
    }

    public static long getTodayBeginTimeStamp() {
        Calendar currentDay = Calendar.getInstance();
        currentDay.set(11, 0);
        currentDay.set(12, 0);
        currentDay.set(13, 0);
        currentDay.set(14, 0);
        return currentDay.getTime().getTime();
    }

    public static long getTodayEndTimeStamp() {
        Calendar currentDay = Calendar.getInstance();
        currentDay.set(11, 23);
        currentDay.set(12, 59);
        currentDay.set(13, 59);
        currentDay.set(14, 999);
        return currentDay.getTime().getTime();
    }

    public static int getTodayBeginSecondTimeStamp() {
        long beginTimeStamp = getTodayBeginTimeStamp();
        return (int)(beginTimeStamp / 1000L);
    }

    public static int getTodayEndSecondTimeStamp() {
        long endTimeStamp = getTodayEndTimeStamp();
        return (int)(endTimeStamp / 1000L);
    }

    public static String getYearMonDayPath() {
        String pathYear = getCurrentYear() + File.separator;
        String pathMon = getCurrentMonth() + File.separator;
        String pathDay = getCurrentDay() + File.separator;
        String path = pathYear + pathMon + pathDay;
        return path;
    }

    public static Date deFormat(String dateValue, String pattern) {
        Date date = new Date(0L);

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
            date = dateFormat.parse(dateValue);
        } catch (Exception var4) {
        }

        return date;
    }

    public static int compare(Date date1, Date date2) {
        if (date1 == null) {
            return -1;
        } else if (date2 == null) {
            return 1;
        } else if (date1.getTime() > date2.getTime()) {
            return 1;
        } else {
            return date1.getTime() < date2.getTime() ? -1 : 0;
        }
    }

    public static void main(String[] args) {
        Date value = new Date();
        System.out.println(formatDate(value));
        System.out.println(formatDateTime(value));
        System.out.println(formatTime(value));
        System.out.println(formatMillSecondDateTime(value));
        System.out.println(format(value, "yyyyMMdd"));
        System.out.println(value.getTime());
        System.out.println(0.01D);
        System.out.println(getSecondTimeStamp(value));
        System.out.println(File.separator);
        System.out.println(getYesterdayString());
        Date yesterday = getYesterdayDate();
        System.out.println("yesterday=" + formatDateTime(yesterday));
        System.out.println("CurrentYear=" + getCurrentYear());
        System.out.println("CurrentMonth=" + getCurrentMonth());
        System.out.println("CurrentDay=" + getCurrentDay());
        System.out.println("Today Start TimeStamp=" + getTodayBeginTimeStamp());
        System.out.println("Today End TimeStamp=" + getTodayEndTimeStamp());
        System.out.println("Today Start SecondTimeStamp=" + getTodayBeginSecondTimeStamp());
        System.out.println("Today End SecondTimeStamp=" + getTodayEndSecondTimeStamp());
    }
}
